﻿using System;
using System.Collections.Generic;
using Monkey.Warrenty.Data.Interfaces;
using Monkey.Warrenty.EntityModel;
using System.Linq;
using Monkey.Warrenty.Enum;

namespace Monkey.Warrenty.Data
{
    public class WarrantyProductDao : IWarrantyProductDao
    {
        public List<WarrantyTicketProductView> GetWarrantyTicketProducts(int statusId, int startIndex, int pageSize, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<WarrantyTicketProductView> query = context.WarrantyTicketProductViews;

                // filters
                if (statusId > 0)
                {
                    query = query.Where(x => x.WarrantyProductStatusId == statusId);
                }

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public List<WarrantyTicketProductView> GetWarrantyTicketProductsByFilter(int statusId, int startIndex, int pageSize, string sorting, string name, string code, string ticketCode)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                IEnumerable<WarrantyTicketProductView> query = context.WarrantyTicketProductViews;

                // filters
                if (statusId > 0)
                {
                    query = query.Where(x => x.WarrantyProductStatusId == statusId);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Code.IndexOf(code, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(ticketCode))
                {
                    query = query.Where(x => x.TicketCode.IndexOf(ticketCode, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public void UpdateProcessProduct(WarrantyTicketProductView productView)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                var product = db.WarrantyTickets_Products.FirstOrDefault(x => x.Id == productView.Id);
                if (product == null) return;

                product.EmployeeId = productView.EmployeeId;
                product.WarrantySupplierId = productView.WarrantySupplierId;
                product.Description = productView.Description;

                db.SaveChanges();
            }
        }

        public void UpdateProductStates(List<long> productIds, int statusId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                foreach (var productId in productIds)
                {
                    context.WarrantyTicketProduct_UpdateStatus(productId, statusId);
                }
            }
        }

        public void SendToWarrantyDep(List<long> productIds, int employeeId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                foreach (var productId in productIds)
                {
                    context.WarrantyProduct_SendToWarrantyDep(productId, employeeId);
                }
            }
        }

        public void SendToTechnical(List<long> productIds, int employeeId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                foreach (var productId in productIds)
                {
                    context.WarrantyProduct_SendToTechnical(productId, employeeId);
                }
            }
        }

        public void SendToSupplier(long id, int supplierId, string note, DateTime? sentDate, DateTime? wishDate)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.WarrantyProduct_SendToSupplier(id, supplierId, note, sentDate, wishDate);
            }
        }

        public void SendToSupplier(List<long> ids, int supplierId, string note)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                foreach (var id in ids)
                {
                    context.WarrantyProduct_SendToSupplier(id, supplierId, note, null, null);
                }
            }
        }

        public void ReceivedCompleted(long id, int supplierId, string note, DateTime? sentDate, DateTime? wishDate, DateTime? receivedDate)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                context.WarrantyProduct_ReceivedCompleted(id, supplierId, note, sentDate, wishDate, receivedDate);
            }
        }

        public void UpdateLendProductStates(List<long> productIds, int statusId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                foreach (var productId in productIds)
                {
                    context.WarrantyTicketLendProduct_UpdateStatus(productId, statusId);
                }
            }
        }

        public int GetWarrantyProductsCount(int statusId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.WarrantyTicketProductViews.Count(x => x.WarrantyProductStatusId == statusId);
            }
        }

        public int GetWarrantyProductsCountByFilter(int statusId, string name, string code, string ticketCode)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                IEnumerable<WarrantyTicketProductView> query = db.WarrantyTicketProductViews;

                if (statusId > 0)
                {
                    query = query.Where(x => x.WarrantyProductStatusId == statusId);
                }
                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Code.IndexOf(code, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(ticketCode))
                {
                    query = query.Where(x => x.TicketCode.IndexOf(ticketCode, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                return query.Count();
            }
        }

        public int GetLendProductsCount(int statusId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.WarrantyTicketLendProductViews.Count(x => x.LendProductStatusId == statusId);
            }
        }

        public List<WarrantyTicketLendProductView> GetLendProducts(int statusId, int startIndex, int pageSize, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                var query = from p in context.WarrantyTicketLendProductViews
                            where p.LendProductStatusId == statusId
                            select p;
                //IEnumerable<WarrantyTicketProductView> query = context.WarrantyTicketProductViews;

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("ProductName ASC"))
                {
                    query = query.OrderBy(x => x.ProductName);
                }
                else if (sorting.Equals("ProductName DESC"))
                {
                    query = query.OrderByDescending(x => x.ProductName);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public int GetProcessingWarrantyProductsCount()
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.WarrantyTicketProductViews.Count(x => x.WarrantyProductStatusId == 8
                    || x.WarrantyProductStatusId == 3); // At technical or at supplier
            }
        }

        public int GetProcessingWarrantyProductsCount(int employeeId)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                return context.WarrantyTicketProductViews.Count(x => (x.EmployeeId == employeeId) &&
                    (x.WarrantyProductStatusId == 2 || x.WarrantyProductStatusId == 3)); // At technical or at supplier
            }
        }

        public List<WarrantyTicketProductView> GetProcessingWarrantyProducts(int startIndex, int pageSize, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                var query = from p in context.WarrantyTicketProductViews
                            where p.WarrantyProductStatusId == 2 || p.WarrantyProductStatusId == 3
                            select p;
                //IEnumerable<WarrantyTicketProductView> query = context.WarrantyTicketProductViews;

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public List<WarrantyTicketProductView> GetProcessingWarrantyProducts(int employeeId, int startIndex, int pageSize, string sorting)
        {
            using (var context = new MonkeyWarrantyEntities())
            {
                var query = from p in context.WarrantyTicketProductViews
                            where (p.EmployeeId == employeeId) && (p.WarrantyProductStatusId == 2 || p.WarrantyProductStatusId == 3)
                            select p;
                //IEnumerable<WarrantyTicketProductView> query = context.WarrantyTicketProductViews;

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public WarrantyTicketProductView GetWarrantyTicketProductView(long id)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                return db.WarrantyTicketProductViews.FirstOrDefault(x => x.Id == id);
            }
        }

        public List<WarrantyTicketProductView> GetReceivedWarrantyProductsByFilter(int startIndex, int pageSize, string sorting, string name, string code, string ticketCode)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                IEnumerable<WarrantyTicketProductView> query = db.WarrantyTicketProductViews;

                query = query.Where(x => x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedCompleted
                    || x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedNotCompleted);

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Code.IndexOf(code, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(ticketCode))
                {
                    query = query.Where(x => x.TicketCode.IndexOf(ticketCode, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                // sorting
                if (sorting.Equals("Code ASC"))
                {
                    query = query.OrderBy(x => x.Code);
                }
                else if (sorting.Equals("Code DESC"))
                {
                    query = query.OrderByDescending(x => x.Code);
                }
                else if (sorting.Equals("Name ASC"))
                {
                    query = query.OrderBy(x => x.Name);
                }
                else if (sorting.Equals("Name DESC"))
                {
                    query = query.OrderByDescending(x => x.Name);
                }

                return pageSize > 0
                        ? query.Skip(startIndex).Take(pageSize).ToList() //Paging
                        : query.ToList(); //No paging
            }
        }

        public int GetReceivedWarrantyProductsCountByFilter(string name, string code, string ticketCode)
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                IEnumerable<WarrantyTicketProductView> query = db.WarrantyTicketProductViews;

                query = query.Where(x => x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedCompleted
                    || x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedNotCompleted);

                if (!string.IsNullOrEmpty(name))
                {
                    query = query.Where(x => x.Name.IndexOf(name, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(code))
                {
                    query = query.Where(x => x.Code.IndexOf(code, StringComparison.OrdinalIgnoreCase) >= 0);
                }
                if (!string.IsNullOrEmpty(ticketCode))
                {
                    query = query.Where(x => x.TicketCode.IndexOf(ticketCode, StringComparison.OrdinalIgnoreCase) >= 0);
                }

                return query.Count();
            }
        }

        public int GetReceivedWarrantyProductsCount()
        {
            using (var db = new MonkeyWarrantyEntities())
            {
                var count = db.WarrantyTicketProductViews.Count(x => x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedCompleted
                    || x.WarrantyProductStatusId == (int)WarrantyProductStatus.ReceivedNotCompleted);

                return count;
            }
        }
    }
}
